/**
 * 从上到下按层打印二叉树，同一层的节点按从左到右的顺序打印，每一层打印到一行。
 * 层序打印
 */

// 广度优先 BFS

var levelOrder = function(root) {
    if (root == null) return [];
    let res = [];
    let queue = [];
    queue.push(root)
    while (queue.length) {
        let temp = [];
        for (let i = queue.length; i > 0; i--) {
            let node = queue.shift();
            temp.push(node.val)
            if (node.left) queue.push(node.left);
            if (node.right) queue.push(node.right);
        }
        res.push(temp)
    }
    return res;
};